# -*- coding: utf-8 -*-
#coding=utf-8

import web
import os
import config
from log import log
from action.base import htmlAction
from model.user import user

'''
Admin controller: used for producing views for desktop clients.
'''
class userAction(htmlAction):
    def __init__(self, name=config.NAME, chkLogin=True, chkInstall=True):
        htmlAction.__init__(self, name)

        self.privData = {
            'NAME':         self.name,
            'STATUS_LIST':  { 1:'发布', 0: '草稿'},
            'GENDER_LIST':  { 1:'男', 0: '女'},
        }

        self.globalsTmplFuncs['getUserNickName'] = self.getUserNickName
        self.globalsTmplFuncs['getProductName'] = self.getProductName
        self.globalsTmplFuncs['getCompanyName'] = self.getCompanyName
        self.tmplDir = os.path.join(config.TMPL_DIR, 'user', config.USER_TEMPLATE)
        self.render = web.template.render(self.tmplDir, globals=self.globalsTmplFuncs)
        self.privData['render'] = self.render

        if self.isLogin() == 'admin':
            raise web.seeother(self.makeUrl('/admin'))
        elif chkLogin and not self.isLogin():
            raise web.seeother(self.makeUrl('/user/login'))
        if self.isLogin():
            self.userid = int(self.getUserId())

    def getUserNickName(self, userid):
        from model.user import user
        info = user().getOne('nickname',{'id':userid})
        return info['nickname']

    def getProductName(self, id):
        from model.product import product
        info = product().getOne('name',{'id':id})
        return info['name']

    def getCompanyName(self, userid):
        from model.company import company
        info = company().getOne('name',{'id':userid})
        return info['name']

    def getPageStr(self, url, currentPage, perPageCount, totalCount=10000):
        totalPage = totalCount/perPageCount
        totalPage += 1 if totalCount%perPageCount else 0

        if '?' in url:
            url=url+'&page='
        else:
            url=url+'?page='

        pageString= ''

        if currentPage > 1:
            pageString += '''
                <span class="alignleft"><a href="'''+ url + str(currentPage-1)+'''"> &laquo;上页</a></span>
            '''
        if totalPage > currentPage:
            pageString += '''
            <span class="alignright"><a href="''' + url + str(currentPage+1)+'''"> 下页 &raquo;</a></span>
            '''
        return pageString

    def getUserId(self):
        userObj = user().getOne('id', {'email': self.isLogin()})
        return userObj['id']

    def getNewsCats(self):
        from model.category import category
        from model.news import news
        newslist = news().getList('id,category', 'category>1')
        ids = list()
        for news in newslist:
            ids.append(news['category'])

        categories = list()
        for id in ids:
            cat = category().getOne('*', {'id': id})
            categories.append(cat)
        return categories

    def getInfoCats(self):
        from model.category import category
        from model.info import info
        infolist = info().getList('id,category', 'category>1')
        ids = list()
        for info in infolist:
            ids.append(info['category'])

        categories = list()
        for id in ids:
            cat = category().getOne('*', {'id': id})
            categories.append(cat)
        return categories

    def getProdCats(self):
        from model.category import category
        from model.product import product
        prodlist = product().getList('id,category', 'category>1')
        ids = list()
        for prod in prodlist:
            ids.append(prod['category'])

        categories = list()
        for id in ids:
            cat = category().getOne('*', {'id': id})
            categories.append(cat)
        return categories

    def getCompCats(self):
        from model.category import category
        from model.company import company
        complist = company().getList('id,category', 'category>1')
        ids = list()
        for comp in complist:
            ids.append(comp['category'])

        categories = list()
        for id in ids:
            cat = category().getOne('*', {'id': id})
            categories.append(cat)
        return categories


